import { createRouter, createWebHashHistory } from 'vue-router'
import HomePage from '@/views/HomePage.vue'
import VisoPage from '@/views/VisoPage.vue'
import axios from 'axios';
import { useStore } from 'vuex';
const store = useStore();

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomePage
  },
  {
    path: '/viso',
    name: 'viso',
    component: VisoPage
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

// 导航守卫
router.beforeEach((to, from, next) => {
  // 在访问首页时执行一些拦截逻辑
  if (to.path === '/') {

      const currentPath = from.fullPath;

      // 在控制台输出当前路由的路径
      console.log('Current Path:', currentPath);

      // 这里可以添加你的逻辑，例如重定向到另一个页面
      console.log('拦截到首页请求，执行一些逻辑');

      var url = "api/user/visiteLog";
      var postObj = { };
      axios.post(url, postObj).then(res => {
          let authToken = "longxxxx";
          console.log(res.data);
      }).catch(function (error) {
          console.log(error);
      })

      // 重定向到首页
      // next('/home');
      // next('/');
      next();
  }
  else {
      // 其他情况正常放行
      next();
  }
});



export default router
